(I) Background
- Instructor: Peng Wang, AVP, Head of Data Science - Operation & Fraud Detection at MassMutual Financial Group (Fall 2016)
- The data set used in this project is gapminder from bioconnector.org. The data set includes the data of Life Expectancy, Population, GDP per Capita of all the countries of each continent from 1952 to 2007 with an interval of 5 years.
- Download Data
- Multiple visualizations of the data set (Section II) and an interactive web application (Section III) were made in this project report.
(II) Visulizations
gapminder=read.csv("https://raw.githubusercontent.com/HarryLu95/dalu/master/Project%20Files/gapminder.csv")
colnames(gapminder)=c("Country","Continent","Year","Life.Expectancy","Population","GDP.per.Capita")
1. How many unique countries are represented per continent?
table.1=gapminder %>%
group_by(Continent) %>%
summarise(Country=n_distinct(Country))
kable(table.1,col.names=c("Continent","Country"),align="c")
| Africa |
52 |
| Americas |
25 |
| Asia |
33 |
| Europe |
30 |
| Oceania |
2 |
ggplot(data=table.1)+
geom_col(mapping=aes(Continent,Country),width=0.5)+
ggtitle("Country Number of Each Continent")+
xlab("Continent")+
ylab("Country")+
theme(plot.title=element_text(size=20,hjust=0.5))

2. According to the data available, what was the average Life Expectancy across each continent from 1952 to 2007?
le=tapply(gapminder$Life.Expectancy,list(gapminder$Continent,gapminder$Year),mean)
le=data.frame(t(le))
Year=seq(from=1952,to=2007,by=5)
le=cbind(Year,le)
le=gather(data=le,Continent,Average.Life.Expectancy,-Year)
p1=ggplot(data=le,aes(x=Year,y=Average.Life.Expectancy,color=Continent))+
geom_point()+
geom_line()+
ggtitle("Average Life Expectancy")+
xlab("Year")+
ylab("Life Expectancy (Years)")+
theme(plot.title=element_text(size=20,hjust=0.5))
p1

2.1 Life Expectancy for Every Countries in Americas
le.Americas=gapminder%>%filter(Continent=="Americas")
le.Americas=tapply(le.Americas$Life.Expectancy,list(le.Americas$Country,le.Americas$Year),mean)
le.Americas=t(data.frame(le.Americas))
row.names(le.Americas)=seq(1952,2007,5)
matplot(seq(1952,2007,5),le.Americas,type="l",lty=1,xlab="Years",ylab="Life Expectancy")

2.2 Countries that have the longest average Life Expectancy in the world
lle=sort(tapply(gapminder$Life.Expectancy,gapminder$Country,mean),decreasing=TRUE)[1:5]
lle=as.data.frame(lle)
colnames(lle)="Average Life Expectancy"
kable(lle,align="c")
| Iceland |
76.51142 |
| Sweden |
76.17700 |
| Norway |
75.84300 |
| Netherlands |
75.64850 |
| Switzerland |
75.56508 |
2.3 Countries that have the shortest average Life Expectancy in the world
sle=sort(tapply(gapminder$Life.Expectancy,gapminder$Country,mean),decreasing=FALSE)[1:5]
sle=as.data.frame(sle)
colnames(sle)="Average Life Expectancy"
kable(sle,align="c")
| Sierra Leone |
36.76917 |
| Afghanistan |
37.47883 |
| Angola |
37.88350 |
| Guinea-Bissau |
39.21025 |
| Mozambique |
40.37950 |
3. According to the data available, what was the average Population across each continent from 1952 to 2007?
pop=tapply(gapminder$Population,list(gapminder$Continent,gapminder$Year),mean)
pop=data.frame(t(pop))
Year=seq(from=1952,to=2007,by=5)
pop=cbind(Year,pop)
pop=gather(data=pop,Continent,Population,-Year)
p2=ggplot(data=pop,aes(x=Year,y=Population,color=Continent))+
geom_point()+
geom_line()+
ggtitle("Average Population")+
xlab("Year")+
ylab("Population")+
theme(plot.title=element_text(size=20,hjust=0.5))
p2

3.1 Population for Every Countries in Americas
pop.Americas=gapminder%>%filter(Continent=="Americas")
pop.Americas=tapply(pop.Americas$Population,list(pop.Americas$Country,pop.Americas$Year),mean)
pop.Americas=t(data.frame(pop.Americas))
row.names(pop.Americas)=seq(1952,2007,5)
matplot(seq(1952,2007,5),pop.Americas,type="l",lty=1,xlab="Years",ylab="Population")

4. According to the data available, what was the average GDP per Capita across each continent from 1952 to 2007?
gdp=tapply(gapminder$GDP.per.Capita,list(gapminder$Continent,gapminder$Year),mean)
gdp=data.frame(t(gdp))
Year=seq(from=1952,to=2007,by=5)
gdp=cbind(Year,gdp)
gdp=gather(data=gdp,Continent,GDP.per.Capita,-Year)
p3=ggplot(data=gdp,aes(x=Year,y=GDP.per.Capita,color=Continent))+
geom_point()+
geom_line()+
ggtitle("Average GDP per Capita")+
xlab("Year")+
ylab("GDP per Capita")+
theme(plot.title=element_text(size=20,hjust=0.5))
p3

4.1 GDP Per Capita for Every Countries in Americas
gdp.Americas=gapminder%>%filter(Continent=="Americas")
gdp.Americas=tapply(gdp.Americas$GDP.per.Capita,list(gdp.Americas$Country,gdp.Americas$Year),mean)
gdp.Americas=t(data.frame(gdp.Americas))
row.names(gdp.Americas)=seq(1952,2007,5)
matplot(seq(1952,2007,5),gdp.Americas,type="l",lty=1,xlab="Years",ylab="GDP Per Capita")
legend("topleft",legend=c("Top 1: America","Top 2: Canada"),lty=1,col=3)

(III) Interactive Web Application
library(shiny)
UI=fluidPage(
titlePanel("World Facts"),
sidebarLayout(
sidebarPanel(
selectInput(inputId="select",
label="Choose a country",
choices=unique(gapminder$Country)
),
selectInput(inputId="object",
label="Choose from the following",
choices=c("Life Expectancy","Population","GDP per Capita")
)
),
mainPanel(plotOutput(outputId="figure"),
tableOutput(outputId="data")
)
)
)
SERVER=function(input,output){
f=function(temp){
result=subset(gapminder,gapminder$Country==temp)
return(result)
}
output$figure=renderPlot({
country.name=reactive(input$select)
dat=f(country.name())
if (input$object=="Life Expectancy"){
plot(dat$Life.Expectancy~dat$Year,xlim=c(1950,2010),xlab="Year",ylab="Life Expectancy",lty=2,type="l",main=c("Life Expectancy of ",country.name()))
points(dat$Life.Expectancy~dat$Year,pch=19,col=1)
}
if (input$object=="Population"){
plot(dat$Population~dat$Year,xlim=c(1950,2010),xlab="Year",ylab="Population",lty=2,type="l",main=c("Population of ",country.name()))
points(dat$Population~dat$Year,pch=19,col=1)
}
if (input$object=="GDP per Capita"){
plot(dat$GDP.per.Capita~dat$Year,xlim=c(1950,2010),xlab="Year",ylab="GDP per Capita",lty=2,type="l",main=c("GDP per Capita of ",country.name()))
points(dat$GDP.per.Capita~dat$Year,pch=19,col=1)
}
})
output$data=renderTable(colnames=T,{
country.name=reactive(input$select)
dat=f(country.name())
if (input$object=="Life Expectancy"){
temp=c()
temp$Year=dat$Year
temp$`Life Expectancy`=dat$Life.Expectancy
return(temp)
}
if (input$object=="Population"){
temp=c()
temp$Year=dat$Year
temp$Population=dat$Population
return(temp)
}
if (input$object=="GDP per Capita"){
temp=c()
temp$Year=dat$Year
temp$`GDP per Capita`=dat$GDP.per.Capita
return(temp)
}
})
}
shinyApp(ui=UI,server=SERVER)